/**
 * User: zsquirrel
 * Date: 2020/5/1
 * Time: 10:04 上午
 */
package com.cskaoyan.mall.controller.admin;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.model.bo.ChangeOrderBO;
import com.cskaoyan.mall.model.bo.OrderRequestParamBO;
import com.cskaoyan.mall.model.vo.OrderInfoVO;
import com.cskaoyan.mall.model.vo.OrderResponseVO;
import com.cskaoyan.mall.model.Result;
import com.cskaoyan.mall.service.OrderService;
import com.cskaoyan.mall.service.OrderServiceImpl;
import com.cskaoyan.mall.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/api/admin/order/*")
public class OrderServlet extends HttpServlet {

    Gson gson = new Gson();

    private OrderService orderService = new OrderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if("order".equals(action)){
            order(req, resp);
        }else if("deleteOrder".equals(action)){
            deleteOrder(req, resp);
        }
    }

    /**
     * 可以逻辑删除，也可以物理删除
     * @param req
     * @param resp
     */
    private void deleteOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        orderService.deleteOrder(id);
        resp.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 显示订单详情
     * @param request
     * @param response
     */
    private void order(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        OrderInfoVO orderInfo = orderService.getOrderInfo(id);
        response.getWriter().println(gson.toJson(Result.ok(orderInfo)));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if("ordersByPage".equals(action)){
            ordersByPage(req, resp);
        }else if("changeOrder".equals(action)){
            changeOrder(req, resp);
        }
    }

    /**
     * 修改订单逻辑，最低要求仅修改订单状态，其他可以不用去修改
     * @param req
     * @param resp
     * @throws IOException
     */
    private void changeOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtils.getRequestBody(req);
        ChangeOrderBO changeOrderBO = gson.fromJson(requestBody, ChangeOrderBO.class);
        orderService.changeOrder(changeOrderBO);
        resp.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 分页同时多条件查询显示订单
     * @param req
     * @param resp
     */
    private void ordersByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtils.getRequestBody(req);
        OrderRequestParamBO param = gson.fromJson(requestBody, OrderRequestParamBO.class);
        //进行一些校验 currentPage pagesize必须要有值
        try {
            if(!StringUtils.isEmpty(param.getMoneyLimit1())){
                Double.parseDouble(param.getMoneyLimit1());
            }
            if(!StringUtils.isEmpty(param.getMoneyLimit2())){
                Double.parseDouble(param.getMoneyLimit2());
            }
        }catch (Exception e){
            //直接return一个10000
            return;
        }
        OrderResponseVO orderResponseVO = orderService.ordersByPage(param);
        Result result = new Result();
        result.setCode(0);
        result.setData(orderResponseVO);
        resp.getWriter().println(gson.toJson(result));
    }
}
